﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MarketSimulator.Agents;
using MarketSimulator.Agents.Agents;
using jade.wrapper;
using MarketSimulator.UI.Assistant;
using MarketSimulator.Utils.Maps;

namespace MarketSimulator.UI
{
    public partial class MainForm : Form
    {
        AgentCreator AgentCreator;

        AgentController AssistantAgentController;

        AssistantAgent AssistantAgent;

        public MainForm()
        {
            InitializeComponent();
            AgentCreator = new AgentCreator(this);
                        
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            InitAgents();
        }

        private void InitAgents()
        {
            AgentCreator.StartBuyerAgents(WriteToBuyerChat, WriteToAgentsChat, "BUY_1", "BUY_2", "BUY_3", "BUY_4", "BUY_5", "BUY_6", "BUY_7", "BUY_8");
            AgentCreator.StartSellerAgents(WriteToSellerChat, WriteToAgentsChat, "SELL_1", "SELL_2", "SELL_3", "SELL_4", "SELL_5", "SELL_6");
            AgentCreator.StartStorageAgents(WriteToStorageChat, WriteToAgentsChat, "DEP_1", "DEP_2", "DEP_3", "DEP_4", "DEP_5", "DEP_6");
            AgentCreator.StartTransporterAgents(WriteToTransporterChat, WriteToAgentsChat, "TRAN_1", "TRAN_2", "TRAN_3", "TRAN_4", "TRAN_5", "TRAN_6", "TRAN_7", "TRAN_8", "TRAN_9");
            AssistantAgentController = AgentCreator.GetAssistantAgent(WriteToAgentsChat, "MarketSimulator.UI.Assistant.AssistantAgent");
        }

        #region Write To Chat Methods
        
        public void WriteToAgentsChat(string message)
        {
            this.textBoxAgentsChat.Text += message + Environment.NewLine;
            this.textBoxAgentsChat.Update();
        }

        public void WriteToSellerChat(string message)
        {
            this.textBoxSellerChat.Text += message + Environment.NewLine + Environment.NewLine;
            this.textBoxSellerChat.Update();
        }

        public void WriteToBuyerChat(string message)
        {
            this.textBoxBuyerChat.Text += message + Environment.NewLine + Environment.NewLine;
            this.textBoxBuyerChat.Update();
        }

        public void WriteToTransporterChat(string message)
        {
            this.textBoxTransporterChat.Text += message + Environment.NewLine + Environment.NewLine;
            this.textBoxTransporterChat.Update();
        }

        public void WriteToStorageChat(string message)
        {
            this.textBoxStorageChat.Text += message + Environment.NewLine + Environment.NewLine;
            this.textBoxStorageChat.Update();
        }

        #endregion

        public void RegisterAssistant(AssistantAgent agent)
        {
            AssistantAgent = agent;
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void buttonSell_Click(object sender, EventArgs e)
        {
            string productQuantity = textBoxQuantitySell.Text;
            string productName = textBoxProductSell.Text;
            int quantity;
            if (int.TryParse(productQuantity, out quantity))
            {
                AssistantAgent.Sell(productName, quantity);
            }
            else
            {
                MessageBox.Show("Quantity must be an integer!");
            }
        }

        private void buttonBuy_Click(object sender, EventArgs e)
        {
            string productQuantity = textBoxQuantityBuy.Text;
            string productName = textBoxProductBuy.Text;
            string depositCity = textBoxDepositBuy.Text;

            int quantity;
            City selectedCity = null;
            foreach (var city in WorldMap.Cities)
            {
                if (city.Name.ToLower().Equals(depositCity.ToLower()))
                {
                    selectedCity = city;
                    break;
                }
            }

            if (selectedCity == null)
            {
                MessageBox.Show("City not found!");
            }

            if (int.TryParse(productQuantity, out quantity))
            {
                AssistantAgent.Buy(productName, quantity, selectedCity);
            }
            else
            {
                MessageBox.Show("Quantity must be an integer!");
            }
        }

       
    }
}
